package com.nutrition.health.service;

import com.nutrition.health.model.KGNode;
import com.nutrition.health.repository.KGNodeRepository;
import com.nutrition.health.repository.KGRelationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class KGService {

    @Autowired
    private KGNodeRepository kgNodeRepository;

    @Autowired
    private KGRelationRepository kgRelationRepository;

    public Map<String, Object> getFoodKGData(String foodName) {
        Map<String, Object> result = new HashMap<>();

        // 查询食品节点
        KGNode foodNode = kgNodeRepository.findByNameAndType(foodName, "食物");
        if (foodNode == null) {
            return null;
        }

        result.put("food", foodNode.getName());
        result.put("description", foodNode.getDescription());

        // 查询食品包含的营养素
        List<KGNode> nutrients = kgNodeRepository.findNutrientsByFoodId(foodNode.getId());
        result.put("nutrients", nutrients);

        // 查询食品直接关联的功效
        List<KGNode> benefits = kgNodeRepository.findBenefitsByFoodId(foodNode.getId());
        result.put("benefits", benefits);

        // 添加关系数据
        List<Map<String, Object>> relations = new ArrayList<>();
        
        // 创建节点名称到节点类型的映射
        Map<String, String> nodeNameToType = new HashMap<>();
        nodeNameToType.put(foodNode.getName(), "FOOD");
        
        for (KGNode nutrient : nutrients) {
            nodeNameToType.put(nutrient.getName(), "NUTRIENT");
            Map<String, Object> relation = new HashMap<>();
            relation.put("source", foodNode.getName());
            relation.put("target", nutrient.getName());
            relation.put("type", "CONTAINS");
            relations.add(relation);
        }
        
        for (KGNode benefit : benefits) {
            nodeNameToType.put(benefit.getName(), "BENEFIT");
            Map<String, Object> relation = new HashMap<>();
            relation.put("source", foodNode.getName());
            relation.put("target", benefit.getName());
            relation.put("type", "HAS_BENEFIT");
            relations.add(relation);
        }
        
        result.put("relations", relations);
        
        // 添加所有节点列表，用于前端渲染
        List<KGNode> allNodes = new ArrayList<>();
        allNodes.add(foodNode);
        allNodes.addAll(nutrients);
        allNodes.addAll(benefits);
        result.put("nodes", allNodes);

        return result;
    }

    public Map<String, Object> getAllKGData() {
        Map<String, Object> result = new HashMap<>();

        // 获取所有食品节点 - 尝试不同的类型值
        List<KGNode> foodNodes = new ArrayList<>();
        foodNodes.addAll(kgNodeRepository.findByType("FOOD"));
        foodNodes.addAll(kgNodeRepository.findByType("食物"));
        result.put("foods", foodNodes);

        // 获取所有营养素节点 - 尝试不同的类型值
        List<KGNode> nutrientNodes = new ArrayList<>();
        nutrientNodes.addAll(kgNodeRepository.findByType("NUTRIENT"));
        nutrientNodes.addAll(kgNodeRepository.findByType("营养素"));
        result.put("nutrients", nutrientNodes);

        // 获取所有功效节点 - 尝试不同的类型值
        List<KGNode> benefitNodes = new ArrayList<>();
        benefitNodes.addAll(kgNodeRepository.findByType("BENEFIT"));
        benefitNodes.addAll(kgNodeRepository.findByType("功效"));
        result.put("benefits", benefitNodes);

        // 获取所有关系
        List<Map<String, Object>> relations = new ArrayList<>();

        // 直接从关系表获取所有关系
        List<com.nutrition.health.model.KGRelation> allRelations = kgRelationRepository.findAll();

        // 创建节点ID到节点名称的映射
        Map<Long, String> nodeIdToName = new HashMap<>();
        for (KGNode node : foodNodes) {
            nodeIdToName.put(node.getId(), node.getName());
        }
        for (KGNode node : nutrientNodes) {
            nodeIdToName.put(node.getId(), node.getName());
        }
        for (KGNode node : benefitNodes) {
            nodeIdToName.put(node.getId(), node.getName());
        }

        // 转换关系数据
        for (com.nutrition.health.model.KGRelation relation : allRelations) {
            String sourceName = nodeIdToName.get(relation.getSourceId());
            String targetName = nodeIdToName.get(relation.getTargetId());

            // 只有当源节点和目标节点都存在时才添加关系
            if (sourceName != null && targetName != null) {
                Map<String, Object> relationMap = new HashMap<>();
                relationMap.put("source", sourceName);
                relationMap.put("target", targetName);
                relationMap.put("type", relation.getRelationType());
                relations.add(relationMap);
            }
        }

        result.put("relations", relations);
        return result;
    }
    
    public List<KGNode> searchFoodsByKeyword(String keyword) {
        return kgNodeRepository.findFoodsByKeyword(keyword);
    }
}